import { createRouter, createWebHashHistory } from 'vue-router'
import { userStore } from '@/stores/user.js'
import { ElMessage } from 'element-plus'
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'layout',
      component: () => import('@/views/layout.vue'),
      children: [
        {
          path: '/',
          redirect: '/home'
        },
        {
          path: '/home',
          name: 'home',
          component: () => import('@/views/home/home.vue')
        },
        {
          path: '/news',
          name: 'newsCenter',
          component: () => import('@/views/home/news.vue')
        },
        {
          path: '/productionShow',
          name: 'productionShow',
          component: () => import('@/views/home/home.vue')
        },
        {
          path: '/recomand',
          name: 'recomand',
          component: () => import('@/views/home/home.vue')
        },
        {
          path: '/service',
          name: 'service',
          component: () => import('@/views/home/home.vue')
        },
        {
          path: '/person',
          name: 'person',
          component: () => import('@/views/home/person.vue'),
          children: [
            {
              path: '/necess',
              name: 'necess',
              component: () => import('@/views/home/person_son/necess.vue')
            },
            {
              path: '/tog',
              name: 'tog',
              component: () => import('@/views/home/person_son/tog.vue')
            }
          ]
        },
        {
          path: '/news',
          name: 'news',
          component: () => import('@/views/home/news.vue')
        },
        {
          path: '/advice/:id',
          name: 'advice',
          component: () => import('@/views/home/advice.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/login.vue')
    },
    {
      path: '/sign',
      name: 'sign',
      component: () => import('@/views/login/sign.vue')
    }
  ]
})

const tokenUrl = ['/news', '/advice', '/person', '/necess', '/tog']

router.beforeEach((to) => {
  const UserStore = userStore()
  if (!UserStore.userInfo.token && tokenUrl.includes(to.path)) {
    ElMessage.warning('您还没有登录，请先去登录')
    return { name: 'login' }
  }

  return true
})

export default router
